﻿using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Waiyun.WebApi
{
    class ThreadPoolTool
    {
        public delegate long OracleDbRunLong(OracleConnection conn);
        public delegate T OracleDbRun<T>(OracleConnection conn);
        public static SingleResult<long> RunSingleLong(string name, string connectionString, OracleDbRunLong run)
        {
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(connectionString, out errorMessage);
                    if (connection != null)
                    {
                        try
                        {
                            long value = run(connection);
                            Cache2.Set(name, value);
                        }
                        catch (Exception ex)
                        {
                            Logger.ErrorToTag("Thread", string.Format("执行数据库操作出错,ex:{0}", ex));
                        }
                        finally
                        {
                            ConnectionPool.Release(connection);
                        }
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<long>(Convert.ToInt64(cacheValue));
            }
            return new SingleResult<long>(0);
        }

        public static SingleResult<T> RunSingle<T>(string name, string connectionString, OracleDbRun<T> run)
        {
            if (ThreadPools.Get(name) == null)
            {
                Thread thread = new Thread(() =>
                {
                    string errorMessage = null;
                    OracleConnection connection = ConnectionPool.GetConnection(connectionString, out errorMessage);
                    if (connection != null)
                    {
                        try
                        {
                            T value = run(connection);
                            Cache2.Set(name, value);
                        }
                        catch (Exception ex)
                        {
                            Logger.ErrorToTag("Thread", string.Format("执行数据库操作出错,ex:{0}", ex));
                        }
                        finally
                        {
                            ConnectionPool.Release(connection);
                        }
                    }
                    ThreadPools.Stop(name);
                });
                thread.Name = name;
                ThreadPools.Start(thread);
            }
            object cacheValue = Cache2.Get(name);
            if (cacheValue != null)
            {
                return new SingleResult<T>((T)cacheValue);
            }
            return new SingleResult<T>(default(T));
        }
    }
}
